#include "MainWindow.h"
#include "canvas.h"
#include "mainmenu.h"
#include <string>
#include <sstream>

MainWindow::MainWindow()
{
    Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup = Gtk::ActionGroup::create();

    m_refActionGroup->add( Gtk::Action::create("MenuFile", "_File") );
    m_refActionGroup->add( Gtk::Action::create("New", Gtk::Stock::NEW));
    m_refActionGroup->add( Gtk::Action::create("Save", Gtk::Stock::SAVE));
    m_refActionGroup->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),sigc::ptr_fun(&Gtk::Main::quit));
    m_refActionGroup->add( Gtk::Action::create("MenuEdit", "_Edit") );
    m_refActionGroup->add( Gtk::Action::create("Undo", Gtk::Stock::UNDO));
    m_refActionGroup->add( Gtk::Action::create("Redo", Gtk::Stock::REDO));
    m_refActionGroup->add( Gtk::Action::create("Cut", Gtk::Stock::CUT));
    m_refActionGroup->add( Gtk::Action::create("Copy", Gtk::Stock::COPY));
    m_refActionGroup->add( Gtk::Action::create("Paste", Gtk::Stock::PASTE));
    m_refActionGroup->add( Gtk::Action::create("MenuHelp", "_Help") );
    m_refActionGroup->add( Gtk::Action::create("Help", Gtk::Stock::HELP));
    m_refActionGroup->add( Gtk::Action::create("About", Gtk::Stock::ABOUT));

    Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
    m_refUIManager->insert_action_group(m_refActionGroup);
    add_accel_group(m_refUIManager->get_accel_group());
    m_refUIManager->add_ui_from_file("ui.xml");
    Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");

    set_size_request(400,400);
    my_view.add(my_draw);
    add(my_vbox);
    my_vbox.pack_start(*pMenubar,Gtk::PACK_SHRINK);
    my_vbox.pack_start(my_view);
    my_vbox.pack_end(my_statusbar,false,false);
    my_draw.signal_motion_notify_event().connect(sigc::mem_fun(*this, &MainWindow::StatusBarUpdate));
    show_all_children();
}

bool MainWindow::StatusBarUpdate(GdkEventMotion * event)
{
    my_statusbar.pop();
    GdkDevice * device = event->device;
    std::ostringstream newmessage (std::ostringstream::out);
    newmessage << "(" << event->x << ", " << event->y << ")";
    if (device->axes)
        for (int i = 0; i < device->num_axes; i++)
        {
            switch ( device->axes[i].use)
            {
                case GDK_AXIS_PRESSURE:
                    newmessage << " Pressure: " << event->axes[i];
                    break;
                case GDK_AXIS_XTILT:
                    newmessage << " X Tilt: " << event->axes[i];
                    break;
                case GDK_AXIS_YTILT:
                    newmessage << " Y Tilt: " << event->axes[i];
                    break;
                default:
                    break;
            }
        }
    my_statusbar.push(newmessage.str());
    return false;
}

bool MainWindow::on_delete_event(GdkEventAny * event)
{
    Gtk::Main::quit();
}

MainWindow::~MainWindow()
{
}
